package com.rename.controller;

import com.feign.common.R;
import com.feign.domain.bo.FileBo;
import com.rename.service.OperateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;

@RestController
@RequestMapping("/operate")
public class OperateController {

    @Autowired
    private OperateService operateService;

    /**
     * 重命名文件
     * @param fileBo
     * @return
     * @throws Exception
     */
    @PutMapping("/rename")
        public R fileRename(@RequestBody FileBo fileBo) throws Exception {
        operateService.fileRename(fileBo.getOldFile(), fileBo.getNewFile());
        return R.ok("修改成功");
    }

    /**
     * 移动文件
     * @param oldFile 旧文件路径
     * @param newFile 新文件路径
     * @return
     * @throws Exception
     */
    @PutMapping("/move")
    public R fileMove(String oldFile, String newFile) throws Exception {
        operateService.fileMove(oldFile, newFile);
        return R.ok("修改成功");
    }

    /**
     * 创建文件
     * @param currentPath 当前路径
     * @param fileName 文件名
     * @return
     */
    @PostMapping("/newFile")
    public R newFile(String currentPath, String fileName) throws Exception {
        operateService.newFile(currentPath, fileName);
        return R.ok("新建文件成功");
    }

    /**
     * 创建文件夹
     * @param currentPath 当前路径
     * @param folderName 文件夹名称
     * @return
     */
    @PostMapping("/newFolder")
    public R newFolder(String currentPath, String folderName) throws Exception {
        operateService.newFolder(currentPath,folderName);
        return R.ok("新建文件夹成功");
    }


    /**
     * 复制文件
     * @param copyFiles 文件名 英文,分割
     * @param copyToPath 目标路径
     * @return
     */
    @PostMapping("/copy")
    public R copyFile(String copyFiles,String copyToPath) throws IOException {
        String[] split = copyFiles.split(",");
        operateService.copyFile(Arrays.asList(split),copyToPath);
        return R.ok("复制成功");
    }

}
